//window.location.reload();=======
//一级导航栏
function initNav(data) {
    $(".sidebar").html("");
    var $nav = $("<ul class='nav nav-list'></ul>");
    for (var i in data) {
        var name = data[i].name;
        var type = data[i].type;
        var picture = data[i].picture;
        if (typeof(data[i].children)=="undefined" || data[i].children.length > 0) {
            var children = data[i].children;
            var $nav1 = $("<li class=''></li>");
            var $a = $("<a href='javascript:void(0)' class='dropdown-toggle'><i class='" + picture + "'></i><span class='menu-text'>" + name + "</span><b class='arrow icon-angle-down'></b></a>");
            $nav1.append($a);
            twoNav($nav1, children, name);
            $nav.append($nav1);
        } else {
            var $nav1 = $("<li class=''></li>")
            var $a = $("<a href='javascript:void(0)'><i class='" + picture + "'></i><span class='menu-text'>" + name + "</span></a>");
            $nav1.bind("click", {obj: data[i]}, function(e) {
                $(".active").removeClass("active");
                $(this).addClass("active");
            });
            $a.bind("click", {obj: data[i]}, function(e) {
                var tip = e.data.obj.name;
                breadcrumb(tip);
                refreshIframe(e.data.obj.path);
            });
            $nav1.append($a);
            $nav.append($nav1);
        }
        $(".sidebar").append($nav);
    }
    $(".sidebar").append("<div class='sidebar-collapse' id='sidebar-collapse'><i class='icon-double-angle-left' data-icon1='icon-double-angle-left' data-icon2='icon-double-angle-right'></i></div>")
}
//二级子导航栏
function twoNav(sidebar, data, title) {
    var $nav = $("<ul class='submenu'></ul>");
    for (var i in data) {
        var name = data[i].name;
        var type = data[i].type;
        if (typeof(data[i].children)=="undefined" || data[i].children.length > 0) {
            var children = data[i].children;
            var $nav1 = $("<li class=''></li>");
            var $a = $("<a href='javascript:void(0)' class='dropdown-toggle'><span class='menu-text'>" + name + "</span><b class='arrow icon-angle-down'></b></a>");
//            $a.bind("click",{name:name},function(e) {
//                var tip = title+","+e.data.name;
//                breadcrumb(tip);
//            });
            $a.bind("contextmenu", function(e) {
                return false;
            });
            $nav1.append($a);
            threeNav($nav1, children, title + "," + name);
            $nav.append($nav1);
        } else {
            var $nav1 = $("<li></li>");
            var $a = $("<a href='javascript:void(0)'><i class='icon-double-angle-right'></i><span class='menu-text'>" + name + "</span></a>");
            $nav1.bind("click", {obj: data[i]}, function(e) {
                $(".active").removeClass("active");
                sidebar.addClass("active")
                $(this).addClass("active");
                $(this).parent().parent().addClass("active");
                if(title == "设备管理"){
                    refreshIframe(e.data.obj.path + e.data.obj.ip);
                }else{
                    refreshIframe(e.data.obj.path);
                }
            });
            $a.bind("click", {obj: data[i]}, function(e) {
                var tip = title + "," + e.data.obj.name;
                breadcrumb(tip);
            });
            $nav1.append($a);
            $nav.append($nav1);
        }
    }
    sidebar.append($nav)
}
//三级子导航栏
function threeNav(sidebar, data, title) {
    var $nav = $("<ul class='submenu'></ul>");
    for (var i in data) {
        var name = data[i].name;
        var type = data[i].type;
        if (typeof(data[i].children)=="undefined" || data[i].children.length > 0) {
            var children = data[i].children;
            var $nav1 = $("<li class=''></li>");
            var $a = $("<a href='javascript:void(0)' class='dropdown-toggle'><span class='menu-text'>" + name + "</span><b class='arrow icon-angle-down'></b></a>");
//            $a.bind("click",{name:name},function(e) {
//                var tip = title+","+e.data.name;
//                breadcrumb(tip);
//            });
            $nav1.append($a);
            fourNav($nav1, children, title + "," + name);
            $nav.append($nav1);
        } else {
            var $nav1 = $("<li></li>");
            var $a = $("<a href='javascript:void(0)'><span class='menu-text'>" + name + data[i].id + "</span></a>");
            $nav1.bind("click", {obj: data[i]}, function(e) {
                $(".active").removeClass("active");
                sidebar.addClass("active");
                $(this).addClass("active");
                $(this).parent().parent().addClass("active");
                $(this).parent().parent().parent().parent().addClass("active");
            });
            $a.bind("click", {obj: data[i]}, function(e) {
                var tip = title + "," + e.data.obj.name;
                breadcrumb(tip);
                refreshIframe(e.data.obj.path+e.data.obj.id);
            });
            $nav1.append($a);
            $nav.append($nav1);
        }
    }
    sidebar.append($nav);
}
//面包屑
function breadcrumb(title) {
    var tip = title.split(",");
    for (var i = 0; i < tip.length; i++) {
        var level = i + 1;
        var value = tip[i];
        $(".breadcrumb li").eq(level).nextAll().hide();
        $(".breadcrumb li").eq(level).html(value);
        $(".breadcrumb li").eq(level).show();
    }
}

//刷新iframe
function refreshIframe(url) {
    if ($.isFunction(url)) {
        url = url.call(this);
    }
    if(url.indexOf("/WebReport/")===-1){
        $("#page-content").attr("scrolling","yes");
        $("#page-content").attr("src", url);
    }else{
        $("#page-content").attr("scrolling","yes");
        $("#page-content").attr("src", url);
    }
    
}

/********************
 * 取窗口滚动条高度 
 ******************/
function getScrollTop()
{
    var scrollTop=0;
    if(document.documentElement&&document.documentElement.scrollTop)
    {
        scrollTop=document.documentElement.scrollTop;
    }
    else if(document.body)
    {
        scrollTop=document.body.scrollTop;
    }
    return scrollTop;
}


/********************
 * 取窗口可视范围的高度 
 *******************/
function getClientHeight()
{
    var clientHeight=0;
    if(document.body.clientHeight&&document.documentElement.clientHeight)
    {
        var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;        
    }
    else
    {
        var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;    
    }
    return clientHeight;
}

/********************
 * 取文档内容实际高度 
 *******************/
function getScrollHeight()
{
    console.log("body.scrollHeight: "+document.body.scrollHeight+" documentElement.scrollHeight:"+document.documentElement.scrollHeight+" getClientHeight:"+getClientHeight()+" getScrollTop:"+getScrollTop());
    return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
}



$(function() {
    initNav(nav);
//    refreshIframe("");

//    $("#page-content").attr("height", $(window).height() - 40);

    var h = document.documentElement.scrollTop + document.documentElement.clientHeight + 100;
//    console.log(h + "px");
    $("#page-content").attr("height", h + "px");

    $(window).bind('resize', function() {
        var h = document.documentElement.scrollTop + document.documentElement.clientHeight + 100;
        $("#page-content").attr("height", h + "px");
    }).trigger('resize');

        
    $(".breadcrumb li").eq(0).nextAll().hide();
    
//    setInterval(function(){
//        
////             $("#page-content").css("height", getScrollTop()+getClientHeight()-40 );
//        var h = document.documentElement.scrollTop + document.documentElement.clientHeight;
//        console.log(h + "px");
//        $("#page-content").attr("height", h + "px");
//    },1000);
    
    
});

//window.location.reload();
